$(document).ready(function () {

    var p = new Pagination();

    $("#searchbtn").click(function () {
        genPage();
    });

    genPage();

    function genPage() {
        Util.ajax({
            type: "POST",
            data: {name: $("#shopname").val()},
            url: "./rest/admin/shoplistcount",
            success: function (data) {
                if (data.status != 0) {
                    return Util.errorData("分页信息获取失败");
                }
                p.init(0, data.data, loadShoplist);
            }
        })
    };

    function loadShoplist() {
        Util.ajax({
            type: "POST",
            data: {offset: p.getOffset(), len: p.getLen(), name: $("#shopname").val()},
            url: "./rest/admin/shoplist",
            success: function (data) {
                if (data.status != 0) {
                    return Util.errorData("信息获取失败");
                }

                var render = _.template($("#tmpl").html());
                var str = "";
                _.each(data.data, function (d) {
                    if (d.ratesum == 0)
                        d.rate = 0;
                    else {
                        d.rate = (d.starsum/ d.ratesum).toPrecision(2);
                    }
                    str += render(d);
                });

                $("#shop-list").html(str);
            }
        });
    };
});

